package com.mineoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mineoj.common.ErrorCode;
import com.mineoj.exception.BusinessException;
import com.mineoj.judge.JudgeService;
import com.mineoj.model.dto.competitionscore.CompetitionScoreAddRequest;
import com.mineoj.model.entity.*;
import com.mineoj.model.enums.QuestionSubmitLanguageEnum;
import com.mineoj.service.CompetitionQuestionService;
import com.mineoj.service.CompetitionService;
import com.mineoj.service.QuestionService;
import com.mineoj.service.CompetitionScoreService;
import com.mineoj.mapper.CompetitionScoreMapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.CompletableFuture;

/**
* @author 明月
* @description 针对表【competition_score(比赛分数)】的数据库操作Service实现
* @createDate 2025-04-20 20:32:43
*/
@Service
public class CompetitionScoreServiceImpl extends ServiceImpl<CompetitionScoreMapper, CompetitionScore>
    implements CompetitionScoreService{

    @Resource
    private QuestionService questionService;
    @Resource
    private CompetitionService competitionService;

    @Resource
    private CompetitionQuestionService competitionQuestionService;

    @Resource
    @Lazy
    private JudgeService judgeService;
    /**
     * 比赛做题
     * @param competitionScoreAddRequest
     * @param loginUser
     * @return
     */
    @Override
    public long doCompetitionQuestionSubmit(CompetitionScoreAddRequest competitionScoreAddRequest, User loginUser) {
        // 校验编程语言是否合法
        String language = competitionScoreAddRequest.getLanguage();
        QuestionSubmitLanguageEnum questionSubmitLanguageEnum = QuestionSubmitLanguageEnum.getEnumByValue(language);
        if (questionSubmitLanguageEnum == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "编程语言非法");
        }

        // 判断题目和比赛是否存在，根据类别获取实体
        long questionId = competitionScoreAddRequest.getQuestionId();
        long competitionId = competitionScoreAddRequest.getCompetitionId();
        Question question = questionService.getById(questionId);
        Competition competition = competitionService.getById(competitionId);
        CompetitionQuestion competitionQuestion = competitionQuestionService.getOne(new QueryWrapper<CompetitionQuestion>()
                .eq("questionId", questionId)
                .eq("competitionId", competitionId));
        if (question == null || competition == null || competitionQuestion == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }

        long userId = loginUser.getId();
        // 每个用户串行提交题目
        //  设置初始状态
        CompetitionScore competitionScore = new CompetitionScore();
        competitionScore.setUserId(userId);
        competitionScore.setCompetitionId(competitionId);
        competitionScore.setQuestionId(questionId);
        competitionScore.setScore(-1);
        boolean save = this.save(competitionScore);
        if (!save) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "数据插入失败");
        }

        long competitionScoreId = competitionScore.getId();
        // 执行判题服务
        CompletableFuture.runAsync(() -> {
            System.out.println("开始执行判题服务");
            judgeService.doCompetitionJudge(competitionScoreId, competitionScoreAddRequest, competitionQuestion);
        });

        return competitionScore.getId();

    }
}




